package com.nstar.base.util
{
	/**
	 * 字符串 工具类
	 */
	public class StringUtils
	{
		/**
		 * 测试content字符串是否以指定的prifix前缀开始
		 */
		public static function startsWith(content:String, prifix:String) : Boolean 
		{
			if(prifix.length > content.length) 
				return false;
			
			return (content.substr(0,prifix.length) == prifix);
			
		}
		
		/**
		 * 删除字符串的最后一个字符
		 */
		public static function removeLastChar(content:String) : String 
		{
			return content.substr(0,content.length-1);
		}
		
		
		/**
		 * 打碎：将一个字符串打碎成一个数组，数组每个单元包含一个字符
		 * 
		 * 反向操作：mashUp
		 */
		public static function breakUp(content:String) : Array
		{
			var array:Array = new Array(content.length);
			
			for( var i:int = 0; i<content.length; i++) {
				array[i] = content.substr(i,1);
			}
			
			return array;
		}
		
		
		/**
		 * 聚合：将一个数组一个字符串成一个字符串
		 * 
		 * 反向操作：breakUp
		 */
		public static function mashUp(array:Array) : String
		{
			var str:String = "";
			for( var i:int = 0; i<array.length; i++) {
				str += array[i].toString();
			}
			
			return str;
		}
		
		
		//忽略大小字母比较字符是否相等; 
		public static function equalsIgnoreCase(char1:String,char2:String):Boolean{
               	return char1.toLowerCase() == char2.toLowerCase();         
		} 
		
		//是否为Email地址;           
		public static function isEmail(char:String):Boolean{
               if(char == null){                   return false;               }
               char = trim(char);              
               var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;               
               var result:Object = pattern.exec(char);              
               if(result == null) {                   return false;               }             
               return true;           
        }   
        
        
         
         //是否为Double型数据;
         public static function isDouble(char:String):Boolean{
         	char = trim(char);   
         	var pattern:RegExp = /^[-\+]?\d+(\.\d+)?$/;  
         	var result:Object = pattern.exec(char);   
         	if(result == null) { 
         		return false;
         	}
         	return true;   
         }
		 
		//是否是Integer;  
		public static function isInteger(char:String):Boolean{   
			 if(char == null){   
			 	 return false;
			 }
			 char = trim(char);   
			 var pattern:RegExp = /^[-\+]?\d+$/;  
			 var result:Object = pattern.exec(char);   
			  if(result == null) {
			  	return false;
			  }
			  return true;
		}
		
		//是否由英文组成English;
		public static function isEnglish(char:String):Boolean{ 
			if(char == null){   
				  return false;
			}
			char = trim(char);   
			var pattern:RegExp = /^[A-Za-z]+$/;
			var result:Object = pattern.exec(char);               
			if(result == null) {   
				return false;
			}
			return true;
		}
		
		//中文
		 public static function isChinese(char:String):Boolean{               
		 	if(char == null){                   
				return false;               
			}   
			char = trim(char);               
			var pattern:RegExp = /^[\u0391-\uFFE5]+$/;                
			var result:Object = pattern.exec(char);               
			if(result == null) {
				return false; 
			}
			return true; 
		 }
		 
		 
		 //含有中文字符  
		 public static function hasChineseChar(char:String):Boolean{ 
		 	if(char == null){   
		 		return false;
		 	}
		 	char = trim(char);               
		 	var pattern:RegExp = /[^\x00-\xff]/;                
		 	var result:Object = pattern.exec(char);               
		 	if(result == null) {
		 		return false;
		 	}
		 	return true;
		 }
		 
		 
		 //URL地址; 
		 public static function isURL(char:String):Boolean{ 
		 	if(char == null){   
		 		return false; 
		 	}
		 	char = trim(char).toLowerCase();               
		 	var pattern:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;               
		 	var result:Object = pattern.exec(char);              
		 	if(result == null) {
		 		return false;
		 	}  
		 	return true;
		 }
		 
		 // 是否为空白;
		 public static function isWhitespace(char:String):Boolean{ 
		 	switch (char){                  
			 	case " ":                  
			 	case "\t":                  
			 	case "\r":                  
			 	case "\n":                  
			 	case "\f":                      
			 	return true;                      
			 	default:                      
			 	return false;             
			 }  
		 }
		 
		 //去左右空格;          
		 public static function trim(char:String):String{              
			 if(char == null){                  
			 return null;              
			 }              
			 return rtrim(ltrim(char));          
		 }  
		 
		 //去左空格;           
		 public static function ltrim(char:String):String{              
			 if(char == null){                  
			 return null;              
			 }              
			 var pattern:RegExp = /^\s*/;               
			 return char.replace(pattern,"");          
		 }  
		 
		  //去右空格;          
		  public static function rtrim(char:String):String{              
			  if(char == null){                  
			  return null;              
			  }              
			  var pattern:RegExp = /\s*$/;               
			  return char.replace(pattern,"");          
		  }  
		 
		 //是否为前缀字符串;          
		 public static function beginsWith(char:String, prefix:String):Boolean{                          
			 return (prefix == char.substring(0, prefix.length));          
		 }   
		                  
		 //是否为后缀字符串;          
		 public static function endsWith(char:String, suffix:String):Boolean{              
		 	return (suffix == char.substring(char.length - suffix.length));          
		 }         
		            
		 //去除指定字符串;          
		 public static function remove(char:String,remove:String):String{              
			 return replace(char,remove,"");          
		 }                    
		 //字符串替换;          
		 public static function replace(char:String, replace:String, replaceWith:String):String{                          
			 return char.split(replace).join(replaceWith);          
		 }  
		 
		 //重复字符
		 public static function repeatStr(char:String, count:int){
		 	var temp:String = "";
		 	for(var i:int = 0;i<count-1;i++){
		 		temp += char;
		 	}
		 	return temp;
		 }
		 
		 //字符串是否为空
		 public static function isEmpty(str:String):Boolean{
		 	return (str == null)||(str=="")||(str==undefined)
		 }
		 
		 function StringUtil(){               
		 	throw new Error("StringUtil class is static container only");           
		 }  
		
		
	}
}